home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / vc / pro3 / rdx1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-05  |  2.9 KB  |  115 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 04-20-93 (23:10)             Number: 255
  4. From: BOB STOUT                    Refer#: NONE
  5.   To: BRUCE PARR                    Recvd: NO  
  6. Subj: ASCII to BINARY CONVERSIO      Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. In a message of <Apr 19 17:18>, Bruce Parr (1:129/179@fidonet) writes:
  9.  
  10.  >Does anyone know of an ASCII to BINARY conversion program for text
  11.  >conversion? I am trying to make one and am having immense headaches, so
  12.  >if one does exist, it will save much time and energy. Please let me know!
  13.  
  14.   From SNIPPETS:
  15.  
  16. ----[ Rdxcnvrt.C ]------------------------------------------------------------
  17. /*
  18. **  RDXCNVRT.C - Convert between number bases
  19. **
  20. **  public domain demo by Bob Stout
  21. */
  22.  
  23. #include <stdlib.h>
  24. #ifdef TEST
  25.  #include <stdio.h>
  26. #endif
  27.  
  28. /*
  29. **  Calling parameters: 1 - Number string to be converted
  30. **                      2 - Buffer for the  converted output
  31. **                      3 - Radix (base) of the input
  32. **                      4 - Radix of the output
  33. **
  34. **  Returns: Pointer to converted output
  35. */
  36.  
  37. char *radix_convert(const char *in, char *out, int rin, int rout)
  38. {
  39.       long n;
  40.       char *dummy;
  41.  
  42.       n = strtol(in, &dummy, rin);
  43.       return ltoa(n, out, rout);
  44. }
  45.  
  46. #ifdef TEST
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50.       int rin, rout;
  51.       char buf[40];
  52.  
  53.       if (4 > argc)
  54.       {
  55.             puts("Usage: RDXCNVRT <number> <base_in> <base_out>");
  56.             return(-1);
  57.       }
  58.       rin  = atoi(argv[2]);
  59.       rout = atoi(argv[3]);
  60.       printf("%s (base %d) = %s (base %d)\n", argv[1], rin,
  61.             radix_convert((const char *)argv[1], buf, rin, rout), rout);
  62.       return 0;
  63. }
  64.  
  65. #endif
  66. ----[ Bstr_I.C ]--------------------------------------------------------------
  67. /*
  68. **  Make an ascii binary string into an integer.
  69. **
  70. **  Public domain by Bob Stout
  71. */
  72.  
  73. #include <string.h>
  74.  
  75. unsigned int bstr_i(char *cptr)
  76. {
  77.       unsigned int i, j = 0;
  78.  
  79.       while (cptr && *cptr && strchr("01", *cptr))
  80.       {
  81.             i = *cptr++ - '0';
  82.             j <<= 1;
  83.             j |= (i & 0x01);
  84.       }
  85.       return(j);
  86. }
  87.  
  88. #ifdef TEST
  89.  
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92.  
  93. int main(int argc, char *argv[])
  94. {
  95.       char *arg;
  96.       unsigned int x;
  97.  
  98.       while (--argc)
  99.       {
  100.             x = bstr_i(arg = *++argv);
  101.             printf("Binary %s = %d = %04Xh\n", arg, x, x);
  102.       }
  103.       return EXIT_SUCCESS;
  104. }
  105.  
  106. #endif /* TEST */
  107. ------------------------------------------------------------------------------
  108.  
  109.  
  110. --- QM v1.00
  111.  * Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
  112. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  113. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  114. SEEN-BY: 390/1 396/1 5 15 2270/1 2440/5 3603/20
  115.